package com.biz.primus.management.coupon.type.tag;

import com.biz.primus.model.coupon.enums.CouponScopeType;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 优惠券使用范围Tag
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class CouponScopeTypeSelectTag extends TagSupport {
    private static final long serialVersionUID = 6305561910035202210L;

    private String selectScopeType;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        for (CouponScopeType scopeType : CouponScopeType.values()) {
            try {
                if (StringUtils.isNotBlank(selectScopeType) && StringUtils.equalsIgnoreCase(selectScopeType, scopeType.name())) {
                    jspWriter.print(String.format("<option value=\"%s\" selected >%s</option>", scopeType.name(), scopeType.getDesc()));
                } else {
                    jspWriter.print(String.format("<option value=\"%s\">%s</option>", scopeType.name(), scopeType.getDesc()));
                }
            } catch (IOException e) {
                log.warn("print jsp content error in CouponScopeTypeSelectTag");
            }
        }
        return super.doStartTag();
    }
}
